{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Faulty Keyboard"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: finalString"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #故障键盘"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>你的笔记本键盘存在故障，每当你在上面输入字符 <code>'i'</code> 时，它会反转你所写的字符串。而输入其他字符则可以正常工作。</p>\n",
    "\n",
    "<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>s</code> ，请你用故障键盘依次输入每个字符。</p>\n",
    "\n",
    "<p>返回最终笔记本屏幕上输出的字符串。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = \"string\"\n",
    "<strong>输出：</strong>\"rtsng\"\n",
    "<strong>解释：</strong>\n",
    "输入第 1 个字符后，屏幕上的文本是：\"s\" 。\n",
    "输入第 2 个字符后，屏幕上的文本是：\"st\" 。\n",
    "输入第 3 个字符后，屏幕上的文本是：\"str\" 。\n",
    "因为第 4 个字符是 'i' ，屏幕上的文本被反转，变成 \"rts\" 。\n",
    "输入第 5 个字符后，屏幕上的文本是：\"rtsn\" 。\n",
    "输入第 6 个字符后，屏幕上的文本是： \"rtsng\" 。\n",
    "因此，返回 \"rtsng\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = \"poiinter\"\n",
    "<strong>输出：</strong>\"ponter\"\n",
    "<strong>解释：</strong>\n",
    "输入第 1 个字符后，屏幕上的文本是：\"p\" 。\n",
    "输入第 2 个字符后，屏幕上的文本是：\"po\" 。\n",
    "因为第 3 个字符是 'i' ，屏幕上的文本被反转，变成 \"op\" 。\n",
    "因为第 4 个字符是 'i' ，屏幕上的文本被反转，变成 \"po\" 。\n",
    "输入第 5 个字符后，屏幕上的文本是：\"pon\" 。\n",
    "输入第 6 个字符后，屏幕上的文本是：\"pont\" 。\n",
    "输入第 7 个字符后，屏幕上的文本是：\"ponte\" 。\n",
    "输入第 8 个字符后，屏幕上的文本是：\"ponter\" 。\n",
    "因此，返回 \"ponter\" 。</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 100</code></li>\n",
    "\t<li><code>s</code> 由小写英文字母组成</li>\n",
    "\t<li><code>s[0] != 'i'</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [faulty-keyboard](https://leetcode.cn/problems/faulty-keyboard/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [faulty-keyboard](https://leetcode.cn/problems/faulty-keyboard/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"string\"', '\"poiinter\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        p=\"\"\n",
    "        for n in s:\n",
    "            if n!=\"i\":\n",
    "                p+=n\n",
    "            else:\n",
    "                p=p[::-1]\n",
    "        return p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        t = True\n",
    "        ans = \"\"\n",
    "        for x in s:\n",
    "            if x == \"i\":\n",
    "                t = not t\n",
    "            elif t:\n",
    "                ans = ans + x\n",
    "            else:\n",
    "                ans = x + ans\n",
    "        if t:\n",
    "            return ans\n",
    "        return ans[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        ans = [] * len(s)\n",
    "        for x in s:\n",
    "            if x != 'i':\n",
    "                ans += x\n",
    "            else:\n",
    "                ans.reverse()\n",
    "        return ''.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        res = \"\"\n",
    "        for i in s:\n",
    "            if i == \"i\":\n",
    "                res = res[::-1]\n",
    "            else:\n",
    "                res += i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "\n",
    "        ans = ''\n",
    "\n",
    "        for i in range(len(s)):\n",
    "            if s[i] != 'i':\n",
    "                ans += s[i]\n",
    "            else:\n",
    "                ans = ''.join(list(reversed(ans)))\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        ans = \"\"\n",
    "        for c in s:\n",
    "            if c == \"i\":\n",
    "                ans = ans[::-1]\n",
    "            else:\n",
    "                ans += c\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        res = []\n",
    "        for x in s:\n",
    "            if x == 'i':\n",
    "                res.reverse()\n",
    "            else:\n",
    "                res.append(x)\n",
    "        return ''.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        ans = ''\n",
    "        for i in s:\n",
    "            if i == 'i':\n",
    "                ans = ans[::-1]\n",
    "                continue\n",
    "            ans += i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        ans = [] * len(s)\n",
    "        for x in s:\n",
    "            if x != 'i':\n",
    "                ans += x\n",
    "            else:\n",
    "                ans.reverse()\n",
    "        return ''.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        cs = \"\"\n",
    "        for i in s:\n",
    "            if i == \"i\":\n",
    "                cs = cs[::-1]\n",
    "            else:\n",
    "                cs = cs + i\n",
    "        return cs\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        ans=''\n",
    "        for i in s:\n",
    "            if i =='i':\n",
    "                ans=ans[::-1]\n",
    "            else:\n",
    "                ans+=i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        ans = []\n",
    "        pox = 0\n",
    "        while pox < len(s):\n",
    "            if s[pox] == 'i':\n",
    "                ans.reverse()\n",
    "            else:\n",
    "                ans.append(s[pox])\n",
    "            pox += 1\n",
    "        return ''.join(i for i in ans)\n",
    "                \n",
    "                \n",
    "                \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        # res= []\n",
    "        tmp=\"\"\n",
    "        for i in range(len(s)):\n",
    "            if s[i]=='i':\n",
    "                tmp=tmp[::-1]\n",
    "            else:\n",
    "                tmp+=s[i]\n",
    "        return tmp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        res = []\n",
    "        flag = True\n",
    "        for c in s:\n",
    "            if c == \"i\":\n",
    "                flag = not flag\n",
    "                continue\n",
    "            if flag:\n",
    "                res.append(c)\n",
    "            else:\n",
    "                res.insert(0, c)\n",
    "        if not flag:\n",
    "            res.reverse()\n",
    "        return \"\".join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "       screen_text = \"\"\n",
    "       for char in s:\n",
    "          if char == 'i':\n",
    "             screen_text = screen_text[::-1]\n",
    "          else:\n",
    "             screen_text += char\n",
    "       return screen_text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        str_=''\n",
    "        for i in s:\n",
    "            if i=='i':\n",
    "                str_=str_[::-1]\n",
    "            else:\n",
    "                str_+=i\n",
    "        return str_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        m = ''\n",
    "        for i in s:\n",
    "            if i != 'i':\n",
    "                m = m + i\n",
    "            else:\n",
    "                m = m[::-1]\n",
    "        return m\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        list1=list(s)\n",
    "        str1=[]\n",
    "        for i in list1:\n",
    "            if i == \"i\":\n",
    "                str1.reverse()\n",
    "                continue\n",
    "            str1+=i\n",
    "        return \"\".join(str1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        ans = \"\"\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == 'i':\n",
    "                ans = ans[::-1]\n",
    "            else:\n",
    "                ans += s[i]\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        new_s = ''\n",
    "        for c in s:\n",
    "            if c == 'i':\n",
    "                new_s = new_s[::-1]\n",
    "            else:\n",
    "                new_s += c\n",
    "        return new_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        ans = ''\n",
    "        for c in s:\n",
    "            if c == 'i':\n",
    "                ans = ans[::-1]\n",
    "                continue\n",
    "            ans += c\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        pre = \"\"\n",
    "        for c in s:\n",
    "            if c != \"i\":\n",
    "                pre += c\n",
    "            else:\n",
    "                pre = pre[::-1]\n",
    "        return pre"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        o = \"\"\n",
    "        for c in s:\n",
    "            if c != \"i\":\n",
    "                o += c \n",
    "            else:\n",
    "                o = o[::-1]\n",
    "        return o"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        res = []\n",
    "        flag = True\n",
    "        for c in s:\n",
    "            if c == \"i\":\n",
    "                flag = not flag\n",
    "                continue\n",
    "            if flag:\n",
    "                res.append(c)\n",
    "            else:\n",
    "                res.insert(0, c)\n",
    "        if not flag:\n",
    "            res.reverse()\n",
    "        return \"\".join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        s = list(s)\n",
    "        l = len(s)\n",
    "        for i in range(l):\n",
    "            if s[i] == \"i\":\n",
    "                s[:i] = s[i-1::-1]\n",
    "                s[i] = '' \n",
    "        return ''.join(s)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        ans = ''\n",
    "        for ch in s:\n",
    "            if ch == 'i':\n",
    "                ans = ans[::-1]\n",
    "            else:\n",
    "                ans = ans + ch\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        t = True\n",
    "        ans = \"\"\n",
    "        for x in s:\n",
    "            if x == \"i\":\n",
    "                t = not t\n",
    "            elif t:\n",
    "                ans = ans + x\n",
    "            else:\n",
    "                ans = x + ans\n",
    "        if t:\n",
    "            return ans\n",
    "        return ans[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        res=''\n",
    "        for i in s:\n",
    "            if i=='i':\n",
    "                res=res[::-1]\n",
    "            else:\n",
    "                res+=i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        res = ''\n",
    "        for single in s:\n",
    "            if single != 'i':\n",
    "                res += single\n",
    "            if single == 'i':\n",
    "                res = res[::-1]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        out = []\n",
    "        \n",
    "        for i in range(len(s)):\n",
    "            if s[i] == 'i':\n",
    "                out = out[::-1]\n",
    "            else:\n",
    "                out.append(s[i])\n",
    "        \n",
    "        ans = \"\".join(out)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        s_ = ''\n",
    "        for i in range(len(s)):\n",
    "            if s[i] != 'i':\n",
    "                s_ += s[i]\n",
    "            else:\n",
    "                s_ = ''.join(reversed(s_))\n",
    "        return s_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        res = ''\n",
    "        for c in s:\n",
    "            if c == 'i':\n",
    "                res = res[::-1]\n",
    "            else:\n",
    "                res += c\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        arr = []\n",
    "        for c in s:\n",
    "            if c == \"i\":\n",
    "                arr.reverse()\n",
    "            else:\n",
    "                arr.append(c)\n",
    "        return \"\".join(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        x = ''\n",
    "        for ele in s:\n",
    "            if ele != 'i':\n",
    "                x += ele\n",
    "            else:\n",
    "                x = x[::-1]\n",
    "        return x\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        res = ''\n",
    "        for i in s:\n",
    "            if i =='i':\n",
    "                res = res[::-1]\n",
    "            else:\n",
    "                res +=i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        ans = \"\"\n",
    "        for c in s:\n",
    "            if c == \"i\":\n",
    "                ans = ans[::-1]\n",
    "            else:\n",
    "                ans += c\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        ans=\"\"\n",
    "        for i in s:\n",
    "            if i ==\"i\":\n",
    "                ans=ans[::-1]\n",
    "            else:\n",
    "                ans+=i\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        ans = ''\n",
    "        for i in s:\n",
    "            if i == 'i':\n",
    "                ans = ans[::-1]\n",
    "            else:\n",
    "                ans += i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        stack = []\n",
    "        i = 0\n",
    "        size = len(s)\n",
    "        while i < size:\n",
    "            if s[i] == 'i':\n",
    "                cnt = 1\n",
    "                i += 1\n",
    "                while i < size and s[i] == 'i':\n",
    "                    i += 1\n",
    "                    cnt += 1\n",
    "                if cnt % 2:\n",
    "                    stack.reverse()\n",
    "            else:\n",
    "                stack.append(s[i])\n",
    "                i += 1\n",
    "        return \"\".join(stack)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        t=''\n",
    "        for x in s:\n",
    "            if x!='i':\n",
    "                t+=x\n",
    "            else:\n",
    "                t=t[::-1]\n",
    "        return t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        t = []\n",
    "        for i in s:\n",
    "            if i == 'i':\n",
    "                t = t[::-1]\n",
    "            else:\n",
    "                t.append(i)\n",
    "        return \"\".join(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        r = \"\"\n",
    "        for i in s:\n",
    "            if i != 'i':\n",
    "                r += i\n",
    "            else:\n",
    "                r = r[::-1]\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        s1 = ''\n",
    "        for i in s:\n",
    "            if i == 'i':\n",
    "                s1 = s1[::-1]\n",
    "            else:\n",
    "                s1 += i\n",
    "        return s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        ans = ''\n",
    "        for c in s:\n",
    "            if c == 'i':\n",
    "                ans = ans[::-1]\n",
    "            else:\n",
    "                ans += c\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        q = deque()\n",
    "        tail = True\n",
    "        for c in s:\n",
    "            if c == 'i':\n",
    "                tail = not tail\n",
    "            elif tail:\n",
    "                q.append(c)\n",
    "            else:\n",
    "                q.appendleft(c)\n",
    "        return ''.join(q if tail else reversed(q))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        ans=''\n",
    "        for i in s:\n",
    "            if i=='i':\n",
    "                ans=ans[::-1]\n",
    "            else:\n",
    "                ans+=i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        ans=''\n",
    "        for i in s:\n",
    "            if i=='i':\n",
    "                ans=ans[::-1]\n",
    "            else:\n",
    "                ans+=i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        s_list = []\n",
    "        for i in s:\n",
    "            if i != 'i':\n",
    "                s_list.append(i)\n",
    "            else:\n",
    "                s_list=s_list[::-1]\n",
    "        return ''.join(s_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        tmp = \"\"\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == \"i\":\n",
    "                tmp = tmp[::-1]\n",
    "            else:\n",
    "                tmp+=s[i]\n",
    "        return tmp\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        ans = \"\"\n",
    "        for c in s:\n",
    "            if c == \"i\":\n",
    "                ans = ans[::-1]\n",
    "            else:\n",
    "                ans += c\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        q = deque()\n",
    "        tail = True\n",
    "        for c in s:\n",
    "            if c == 'i':\n",
    "                tail = not tail\n",
    "            elif tail:\n",
    "                q.append(c)\n",
    "            else:\n",
    "                q.appendleft(c)\n",
    "        return ''.join(q if tail else reversed(q))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        lst = deque()\n",
    "        t = True\n",
    "        for c in s:\n",
    "            if c == \"i\":\n",
    "                t = not t\n",
    "            else:\n",
    "                if t:\n",
    "                    lst.append(c)\n",
    "                else:\n",
    "                    lst.appendleft(c)\n",
    "        return \"\".join(lst if t else reversed(lst))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        res = []\n",
    "        for i in s:\n",
    "            if i != \"i\":\n",
    "                res.append(i) \n",
    "            else:\n",
    "                res.reverse()\n",
    "        return \"\".join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        t = ''\n",
    "        for c in s:\n",
    "            if c == 'i':\n",
    "                t = t[::-1]\n",
    "            else:\n",
    "                t += c\n",
    "        return t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        temp=''\n",
    "        for i in s:\n",
    "            if i !=\"i\":\n",
    "                temp=temp+i\n",
    "            else:\n",
    "                temp=temp[::-1]\n",
    "        return temp\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        a = []\n",
    "        for c in s:\n",
    "            if c=='i':\n",
    "                a.reverse()\n",
    "            else:\n",
    "                a.append(c)\n",
    "        return ''.join(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        ls=[]\n",
    "        for x in s:\n",
    "            if x != \"i\":\n",
    "                ls.append(x)\n",
    "            else:\n",
    "                ls=ls[::-1]\n",
    "        return \"\".join(ls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        buf = \"\"\n",
    "\n",
    "        for char in s:\n",
    "            if char == 'i':\n",
    "                buf = buf[::-1]\n",
    "            else:\n",
    "                buf += char\n",
    "        \n",
    "        return buf\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        res = \"\"\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == \"i\":\n",
    "                res = res[::-1]\n",
    "            else:\n",
    "                res += s[i]\n",
    "        return res\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        ans = []\n",
    "        for c in s:\n",
    "            if c == 'i':\n",
    "                ans.reverse()\n",
    "            else:\n",
    "                ans.append(c)\n",
    "        return \"\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        finSt = ''\n",
    "        for i in s:\n",
    "            if i == 'i':\n",
    "                finSt = finSt[::-1]\n",
    "            else:\n",
    "                finSt = finSt+i\n",
    "        return finSt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        ans = \"\"\n",
    "        for i in s:\n",
    "            if i == \"i\":\n",
    "                ans = ans[::-1]\n",
    "            else:\n",
    "                ans += i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        anw = \"\"\n",
    "        for i in s:\n",
    "            if i == \"i\":anw = anw[::-1]\n",
    "            else:anw += i\n",
    "        return anw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        q = []\n",
    "        for c in s:\n",
    "            if c != 'i':\n",
    "                q.append(c)\n",
    "            else:\n",
    "                q = q[::-1]\n",
    "        return ''.join(q)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        ans = ''\n",
    "        for i in s:\n",
    "            if i == 'i':\n",
    "                ans = ans[::-1]\n",
    "            else:\n",
    "                ans += i\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        s = list(s)\n",
    "        ans = []\n",
    "        for i in s:\n",
    "            if i != 'i':\n",
    "                ans.append(i)\n",
    "            else:\n",
    "                ans.reverse()\n",
    "        return ''.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        temp = []\n",
    "        for ch in s:\n",
    "            if ch is \"i\":\n",
    "                temp.reverse()\n",
    "            else:\n",
    "                temp += ch\n",
    "\n",
    "        return ''.join(temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        q=deque()\n",
    "        tail=True\n",
    "        for c in s:\n",
    "            if c=='i':\n",
    "                tail= not tail\n",
    "            elif tail:\n",
    "                q.append(c)\n",
    "            else:\n",
    "                q.appendleft(c)\n",
    "        return ''.join(q if tail else reversed(q))                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        p=0\n",
    "        while p<len(s):\n",
    "            if s[p]=='i':\n",
    "                s=s[p-1::-1]+s[p+1::]\n",
    "            else:\n",
    "                p+=1\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        ans = ''\n",
    "        for c in s:\n",
    "            if c == 'i':\n",
    "                ans = ans[::-1]\n",
    "            else:\n",
    "                ans += c\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        strs =s.split('i')\n",
    "        res = ''\n",
    "        for t in strs:\n",
    "            \n",
    "            res+=t\n",
    "            res=res[::-1]\n",
    "  \n",
    "        return res[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalString(self, s: str) -> str:\n",
    "        ss=''\n",
    "        for i in s:\n",
    "            if i=='i':\n",
    "                ss=ss[::-1]\n",
    "            else:\n",
    "                ss+=i\n",
    "        return ss"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
